package com.ycz.sas.jgen.domain;

import java.security.MessageDigest;
import java.security.MessageDigestSpi;
import java.security.NoSuchAlgorithmException;

import sun.misc.BASE64Encoder;

import com.kingdee.bos.util.CryptException;
import com.kingdee.bos.util.CryptoUtil;

public class Password {

	public static char[] element1 = { '0', '1', '2', '3', '4', '5', '6', '7',
			'8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
			'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
			'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
			'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
			'Y', 'Z' };
	public static char[] element2 = { '0', '1', '2', '3', '4', '5', '6', '7',
			'8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
			'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
			'y', 'z' };
	public static char[] element3 = { '1', '2', '3', '4', '5', '6', '7', '8',
			'9', '0' };
	public static char[] element4 = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
		'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
		'y', 'z' };
	
	public static char[] element5 = {'\'', '\\', ',', '.', '/', ';', '[', ']', '-', '='};

	public static String decrypt(byte[] b) {
		String rs = "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] b1 = md.digest(b);
			MessageDigestSpi digestSpi = md;

			for (byte b2 : b1) {
				System.out.print(b2 + "  =  =  =  =  = ");
			}
			System.out.println();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return rs;
	}

	public static String encrypt(String strSrc) throws CryptException {
		byte[] btKey = new byte[strSrc.getBytes().length + 1];
		System.arraycopy(strSrc.getBytes(), 0, btKey, 0,
				strSrc.getBytes().length);
		String strEncode;
		byte[] btDigest;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(btKey);
			btDigest = md.digest();
			BASE64Encoder encoder = new BASE64Encoder();
			strEncode = encoder.encode(btDigest);
		} catch (Exception e) {
			throw new CryptException(e.getMessage(), e);
		}
		return strEncode;
	}

	private static boolean yes(String passmd5, String res) {
		return passmd5.equals(res);
	}

	/**
	 * �˹�����
	 * @param user
	 * @param passmd5
	 * @param password
	 */
	public static String allManual_Type(String user, String passmd5, String password) {
		String rs  = null;
		try {
			String res = CryptoUtil.encrypt(user + password);
			if (res.equals(passmd5)) {
				rs = password;
				System.out.println(password);
			} else {
				rs = "failed!";
				System.out.println(rs);
			}
		} catch (CryptException e) {
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * �˹����ַ���
	 * @param user
	 * @param passmd5
	 * @param password
	 */
	public static String manualDigit_Type(String user, String passmd5, String begin,
			String end) {
		String rs = null;
		for (long i = 0; i < 10000000; i++) {
			String password = begin + String.valueOf(i) + end;
			try {
				String res = CryptoUtil.encrypt(user + password);
				if (res.equals(passmd5)) {
					rs = password;
					System.out.println(password);
					break;
				}
				if (i  ==  999999 && !res.equals(passmd5)) {
					System.out.println("failed");
				}
			} catch (CryptException e) {
				e.printStackTrace();
			}
		}
		return rs;
	}

	/**
	 * �ֵ䷽��
	 * @param user
	 * @param passmd5
	 * @param here
	 * @param begin
	 */
	public static String auto_Type(String user, String passmd5, char[] here, String begin, String end)
			throws Exception {
		int size = here.length;
		int state = 0;
		String password = "";
		String res = "";
		String rs = null;
		for (int a = 0; a < size; a++) {
			password = begin + here[a] + end;
			res = CryptoUtil.encrypt(user + password);
			if (yes(passmd5, res)) {
				rs = password;
				System.out.println(password);
				state = 1;
				return rs;
			}
		}
		for (int a = 0; a < size; a++) {
			for (int b = 0; b < size; b++) {
				password = begin + here[a] + here[b]+ end;
				res = CryptoUtil.encrypt(user + password);
				if (yes(passmd5, res)) {
					rs = password;
					System.out.println(password);
					state = 1;
					return rs;
				}
			}
		}
		for (int a = 0; a < size; a++) {
			for (int b = 0; b < size; b++) {
				for (int c = 0; c < size; c++) {
					password = begin + here[a] + here[b] + here[c]+ end;
					res = CryptoUtil.encrypt(user + password);
					if (yes(passmd5, res)) {
						rs = password;
						System.out.println(password);
						state = 1;
						return rs;
					}
				}
			}
		}
		for (int a = 0; a < size; a++) {
			for (int b = 0; b < size; b++) {
				for (int c = 0; c < size; c++) {
					for (int d = 0; d < size; d++) {
						password = begin + here[a] + here[b] + here[c] + here[d]+ end;
						res = CryptoUtil.encrypt(user + password);
						if (yes(passmd5, res)) {
							rs = password;
							System.out.println(password);
							state = 1;
							return rs ;
						}
					}
				}
			}
		}
		for (int a = 0; a < size; a++) {
			for (int b = 0; b < size; b++) {
				for (int c = 0; c < size; c++) {
					for (int d = 0; d < size; d++) {
						for (int e = 0; e < size; e++) {
							password = begin + here[a] + here[b] + here[c]
									+ here[d] + here[e]+ end;
							res = CryptoUtil.encrypt(user + password);
							if (yes(passmd5, res)) {
								rs = password;
								System.out.println(password);
								state = 1;
								return rs;
							}
						}
					}
				}
			}
		}
		for (int a = 0; a < size; a++) {
			for (int b = 0; b < size; b++) {
				for (int c = 0; c < size; c++) {
					for (int d = 0; d < size; d++) {
						for (int e = 0; e < size; e++) {
							for (int f = 0; f < size; f++) {
								password = begin + here[a] + here[b] + here[c]
										+ here[d] + here[e] + here[f]+ end;
								res = CryptoUtil.encrypt(user + password);
								if (yes(passmd5, res)) {
									rs = password;
									System.out.println(password);
									state = 1;
									return rs;
								}
							}
						}
					}
				}
			}
		}
		if (state  ==  0){
			rs = "failed!";
			System.out.println(rs);
		}
		return rs;
	}

	public static void main(String[] args) throws Exception {

		String user = "UhCRAQEdEADgC8UFFAAABBO33n8 = ";
		String passmd5 = "gAPa+ImjR4s2DO+tEh/5cg =  = ";
//		long stime = System.currentTimeMillis();
//
//		try {
//			auto_Type(user, passmd5, element2,"");
//		} catch (CryptException e) {
//			e.printStackTrace();
//		}
//		System.out.println("��ʱ" + (System.currentTimeMillis() - stime) / 1000
//				+ "��");
//		String user = "542490";
//		System.out.println(encrypt(user));
		for (long i = 0; i < 1000; i++) {
			System.out.println(i);
			auto_Type(user, passmd5, element3, "ly"+i,"");
		}
	}
}
